package mmoon.mmoonstory.blocks;

import mmoon.mmoonstory.interfaces.HasModel;
import mmoon.mmoonstory.interfaces.InitBlock;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

import java.util.Random;

public class BlockEmeraldOre extends Block implements InitBlock, HasModel {
    public static final String ObjID = "emerald_ore";

    public BlockEmeraldOre(Material materialIn) {
        super(materialIn);
        this.setHardness(1);
        this.setResistance(6666666);
        this.setHarvestLevel("pickaxe", 2);
        this.init(ObjID);
    }

    public BlockEmeraldOre() {
        this(Material.ROCK);
    }

    @Override
    public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state,
            int fortune) {
        Random rand = world instanceof World ? ((World) world).rand : RANDOM;
        drops.add(new ItemStack(Item.getItemFromBlock(this)));
        int count = this.quantityDropped(state, fortune, rand);
        for (int i = 0; i < count; ++i) {
            Item item = this.getItemDropped(state, rand, fortune);
            if (item != Items.AIR) {
                drops.add(
                        new ItemStack(item, this.quantityDropped(state, fortune, rand)));
            }
        }
    }

    @Override
    public Item getItemDropped(IBlockState state, Random rand, int fortune) {
        float r = rand.nextFloat();
        if (r < 0.5) {
            return Items.COAL;
        } else if (r < 0.75) {
            return Items.REDSTONE;
        } else if (r < 0.875) {
            return Items.DIAMOND;
        } else {
            return Items.EMERALD;
        }
    }

    @Override
    public int getExpDrop(IBlockState state, IBlockAccess world, BlockPos pos, int fortune) {
        Random random = world instanceof World ? ((World) world).rand : new Random();
        return MathHelper.getInt(random, 1, 114514);
    }

    @Override
    public int quantityDropped(IBlockState state, int fortune, Random random) {
        return 1 + random.nextInt(3);
    }

    @Override
    public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos,
            EntityPlayer player) {
        return new ItemStack(this, 64);
    }

    @Override
    public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player,
            boolean willHarvest) {
        this.onBlockHarvested(world, pos, state, player);
        ItemStack inHand = player.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND);
        if (inHand.getItem().getHarvestLevel(inHand, "pickaxe", player, state) >= this.getHarvestLevel(state)
                || player.isCreative()) {
            return world.setBlockState(pos, Blocks.AIR.getDefaultState(), world.isRemote ? 11 : 3);
        } else {
            return world.setBlockState(pos, this.getDefaultState(), world.isRemote ? 11 : 3);
        }
    }

    @Override
    public boolean canSilkHarvest(World world, BlockPos pos, IBlockState state, EntityPlayer player) {
        return true;
    }

    @Override
    public ItemStack getSilkTouchDrop(IBlockState state) {
        return new ItemStack(this, 1);
    }
}
